#ifndef BUSINESSWORKER_H
#define BUSINESSWORKER_H

#include <vector>
#include <atomic>

#include "./Common.h"

#include "Poco/SharedPtr.h"
#include "Poco/Runnable.h"
#include "Poco/Checksum.h"
#include "readerwriterqueue/readerwriterqueue.h"
#include "concurrentqueue/concurrentqueue.h"

namespace Reactor
{

struct MOUDLE_API DataPacket;
class MOUDLE_API BusinessWorker : public Poco::Runnable
{
public:
	BusinessWorker(std::condition_variable& _condi, int _index, std::vector<moodycamel::ConcurrentQueue<std::shared_ptr<DataPacket>>>& _packetQueue,
		const BusinessOp& _businessop);
	void stop();
	bool isHanding()const;

protected:
	virtual void run() override;
	void handle(const std::shared_ptr<DataPacket>& _request);
	bool preemptPakcets(int _max, std::vector<std::shared_ptr<DataPacket>>& _preempts);

private:
	const BusinessOp _businessop;
	std::mutex _mutex;
	std::condition_variable& _condi;
	Poco::Checksum _checksum;
	std::atomic<bool> _stop{ false };
	std::atomic<bool> _isHanding{ false };
	const int _myIndex;
	std::vector<moodycamel::ConcurrentQueue<std::shared_ptr<DataPacket>>>& _packetQueue;
};

}

#endif // !BUSINESSWORKER_H
